package cn.lanqiao.bigDate.reslut;
import cn.lanqiao.bigDate.entity.sys.SysMenu;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class TreeNode {

	private Long id;
	private String name;
	private Boolean checked = false;
	private Long pId;
	private Integer sortOrder;
	private Boolean open=false;
	private Boolean nocheck=false;
	private Boolean isParent=false;

	public Boolean getIsParent() {
		return isParent;
	}

	public void setIsParent(Boolean isParent) {
		this.isParent = isParent;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Boolean getChecked() {
		return checked;
	}

	public void setChecked(Boolean checked) {
		this.checked = checked;
	}

	public Long getpId() {
		return pId;
	}

	public void setpId(Long pId) {
		this.pId = pId;
	}

	public Boolean getOpen() {
		return open;
	}

	public void setOpen(Boolean open) {
		this.open = open;
	}

	public Boolean getNocheck() {
		return nocheck;
	}

	public void setNocheck(Boolean nocheck) {
		this.nocheck = nocheck;
	}

	public Integer getSortOrder() {
		return sortOrder;
	}

	public void setSortOrder(Integer sortOrder) {
		this.sortOrder = sortOrder;
	}

	public TreeNode() {
	}

	public TreeNode(Long id, String name, Long pId) {
		this.id = id;
		this.name = name;
		this.pId = pId;
	}

	/**
	 * 构造树，
	 * @param topName 树顶层节点名称，null：不手动添加顶层节点
	 * @param sysresList 资源列表
	 * @return
	 */
	public static List<TreeNode> convertTreeNodes(List<SysMenu>  sysresList, String topName){
		List<TreeNode> nodeList = new ArrayList<>();
        if(StringUtils.isNotBlank(topName)){
            TreeNode treeNode = new TreeNode(0L,topName,null);
            treeNode.setNocheck(true);
            treeNode.setOpen(true);
            nodeList.add(treeNode);
        }
		if (sysresList == null || sysresList.isEmpty()) {
			return nodeList;
		}
		for (SysMenu sys:sysresList) {
			TreeNode treeNode = new TreeNode();
			treeNode.setId(sys.getId());
			treeNode.setName(sys.getMenuName());
			treeNode.setSortOrder(sys.getOrderNum());
			treeNode.setpId(sys.getParentId()==null?0:sys.getParentId());
			treeNode.setChecked(sys.getChecked());
			nodeList.add(treeNode);
		}

		return nodeList;
	}

	/**
	 * 树构造单节点
	 * @param sysMenu
	 * @
	 */
	public static TreeNode convertTreeNode(SysMenu sysMenu){
		TreeNode treeNode = new TreeNode();
		treeNode.setId(sysMenu.getId());
		treeNode.setName(sysMenu.getMenuName());
		treeNode.setpId(sysMenu.getParentId()==null?0:sysMenu.getParentId());

		return treeNode;
	}
}
